define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'echarts', 'echarts-theme'], function ($, undefined, Backend, Table, Form, Echarts) {

    var Controller = {
        index: function () {
            var queryDate = $("#query_date").val();

            Form.api.bindevent($("form[role=form]"));

            $(document).on("click", ".charts-custom a[data-toggle=\"tab\"]", function () {
                var that = this;
                setTimeout(function () {
                    var id = $(that).attr("href");
                    var chart = Echarts.getInstanceByDom($(id)[0]);
                    chart.resize();
                }, 0);
            });
            var option;
            /*
            var months = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
            var legend = ['去年', '今年', '百分比'];
            var prevYear = '去年'
            var curYear = '今年'
            var maxPerson = 500;
            var maxPercent = 100;
            var prevPersons = [
                    2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3
                  ]
            var curPersons = [
                    2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3
                  ]
            var percent = [34.0, 80.2, 56.3, 87.5, 90, 60, 43.3, 67.4, 88.0, 59.5, 84.0, 30.2]
            */
            var yearToYearData = JSON.parse($("#personYearToYear").val());
            console.log('yearToYearData =>', yearToYearData)
            var barYearToYear = Echarts.init(document.getElementById('bar-chart-depart'), 'walden');
            option = {
              tooltip: {
                trigger: 'axis',
                axisPointer: {
                  type: 'shadow',
                },
                formatter:function(params){
                  // console.log(params)
                  return params[0].seriesName + '年' + params[0].name + "：" + params[0].data + '人次<br/>' + params[1].seriesName + '年' + params[1].name + "：" + params[1].data + '人次<br/>'+params[2].seriesName+ '：' + params[2].data + '%';
                  // return params[0].name  +'<br>生产量 '+ params[0].data;
                }

              },
              toolbox: {
                feature: {
                  saveAsImage: { show: true }
                }
              },
              legend: {
                data: yearToYearData.legend
              },
              xAxis: [
                {
                  type: 'category',
                  data: yearToYearData.months,
                  axisPointer: {
                    type: 'shadow'
                  }
                }
              ],
              yAxis: [
                {
                  type: 'value',
                  name: '人次',
                  min: 0,
                  max: yearToYearData.maxPerson,
                  // interval: 5,
                  axisLabel: {
                    formatter: '{value}'
                  }
                },
                {
                  type: 'value',
                  name: '百分比',
                  min: 0,
                  max: yearToYearData.maxPercent,
                  // interval: 5,
                  axisLabel: {
                    formatter: '{value}%'
                  }
                }
              ],
              series: [
                {
                  name: yearToYearData.prevYear,
                  type: 'bar',
                  tooltip: {
                    valueFormatter: function (value) {
                      console.log('value=>', value)
                      return value + ' 人次';
                    }
                  },
                  data: yearToYearData.prevPersons 
                },
                {
                  name: yearToYearData.curYear,
                  type: 'bar',
                  tooltip: {
                    valueFormatter: function (value) {
                      return value + ' 人次';
                    }
                  },
                  data: yearToYearData.curPersons 
                },
                {
                  name: '同比',
                  type: 'line',
                  yAxisIndex: 1,
                  tooltip: {
                    valueFormatter: function (value) {
                      return value + ' %';
                    }
                  },
                  data: yearToYearData.percent 
                }
              ]
            };
            barYearToYear.setOption(option)
            /*
            var months = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
            var legend = ['今年', '百分比'];
            var curYear = '今年'
            var maxPerson = 500;
            var maxPercent = 100;
            var curPersons = [
                    2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3
                  ]
            var percent = [34.0, 80.2, 56.3, 87.5, 90, 60, 43.3, 67.4, 88.0, 59.5, 84.0, 30.2]
            */

            var personPercent = JSON.parse($("#personPercent").val());
            console.log('personPercent =>', personPercent)
            var barPercent = Echarts.init(document.getElementById('bar-chart-type'), 'walden');
            option = {
              tooltip: {
                trigger: 'axis',
                axisPointer: {
                  type: 'shadow',
                },
                formatter:function(params){
                  // console.log(params)
                  return params[0].name + "：" + params[0].data + '人次<br/>' + params[1].seriesName+ '：' + params[1].data + '%';
                }

              },
              toolbox: {
                feature: {
                  saveAsImage: { show: true }
                }
              },
              legend: {
                data: personPercent.legend
              },
              xAxis: [
                {
                  type: 'category',
                  data: personPercent.months,
                  axisPointer: {
                    type: 'shadow'
                  }
                }
              ],
              yAxis: [
                {
                  type: 'value',
                  name: '人次',
                  min: 0,
                  max: personPercent.maxPerson,
                  // interval: 5,
                  axisLabel: {
                    formatter: '{value}'
                  }
                },
                {
                  type: 'value',
                  name: '百分比',
                  min: 0,
                  max: personPercent.maxPercent,
                  // interval: 5,
                  axisLabel: {
                    formatter: '{value}%'
                  }
                }
              ],
              series: [
                {
                  name: personPercent.curYear,
                  type: 'bar',
                  tooltip: {
                    valueFormatter: function (value) {
                      return value + ' 人次';
                    }
                  },
                  data: personPercent.curPersons 
                },
                {
                  name: '环比',
                  type: 'line',
                  yAxisIndex: 1,
                  tooltip: {
                    valueFormatter: function (value) {
                      return value + ' %';
                    }
                  },
                  data: personPercent.percent 
                }
              ]
            };
            barPercent.setOption(option)

            $(".btn-embossed").on("click", function() {
                queryDate = $("#query_date").val();
                Fast.api.ajax({
                    url:'pt/statistica/index?query_date=' + queryDate
                }, function(data, ret){
                    console.log('statistic =>', ret.data)
                    var res = ret.data
                    yearToYearData = res['personYearToYear'];
                    personPercent = res['personPercent'];

                    barYearToYear.setOption({
                      legend: {
                        data: yearToYearData['legend']
                      },
                      xAxis: [
                        {
                          type: 'category',
                          data: yearToYearData['months'],
                          axisPointer: {
                            type: 'shadow'
                          }
                        }
                      ],
                      yAxis: [
                        {
                          type: 'value',
                          name: '人次',
                          min: 0,
                          max: yearToYearData['maxPerson'],
                          // interval: 5,
                          axisLabel: {
                            formatter: '{value}'
                          }
                        },
                        {
                          type: 'value',
                          name: '百分比',
                          min: 0,
                          max: yearToYearData['maxPercent'],
                          // interval: 5,
                          axisLabel: {
                            formatter: '{value}%'
                          }
                        }
                      ],
                      series: [
                        {
                          name: yearToYearData['prevYear'],
                          type: 'bar',
                          tooltip: {
                            valueFormatter: function (value) {
                              console.log('value=>', value)
                              return value + ' 人次';
                            }
                          },
                          data: yearToYearData['prevPersons'] 
                        },
                        {
                          name: yearToYearData['curYear'],
                          type: 'bar',
                          tooltip: {
                            valueFormatter: function (value) {
                              return value + ' 人次';
                            }
                          },
                          data: yearToYearData['curPersons'] 
                        },
                        {
                          name: '同比',
                          type: 'line',
                          yAxisIndex: 1,
                          tooltip: {
                            valueFormatter: function (value) {
                              return value + ' %';
                            }
                          },
                          data: yearToYearData['percent'] 
                        }
                      ]
                    })
                    barPercent.setOption({
                      legend: {
                        data: personPercent['legend']
                      },
                      xAxis: [
                        {
                          type: 'category',
                          data: personPercent['months'],
                          axisPointer: {
                            type: 'shadow'
                          }
                        }
                      ],
                      yAxis: [
                        {
                          type: 'value',
                          name: '人次',
                          min: 0,
                          max: personPercent['maxPerson'],
                          // interval: 5,
                          axisLabel: {
                            formatter: '{value}'
                          }
                        },
                        {
                          type: 'value',
                          name: '百分比',
                          min: 0,
                          max: personPercent['maxPercent'],
                          // interval: 5,
                          axisLabel: {
                            formatter: '{value}%'
                          }
                        }
                      ],
                      series: [
                        {
                          name: personPercent['curYear'],
                          type: 'bar',
                          tooltip: {
                            valueFormatter: function (value) {
                              return value + ' 人次';
                            }
                          },
                          data: personPercent['curPersons'] 
                        },
                        {
                          name: '环比',
                          type: 'line',
                          yAxisIndex: 1,
                          tooltip: {
                            valueFormatter: function (value) {
                              return value + ' %';
                            }
                          },
                          data: personPercent['percent']
                        }
                      ]
                    })
                }, function(){
                    Backend.api.toastr.error('统计错误');
                });
            })
        },
        fee: function () {
            var queryDate = $("#query_date").val();

            Form.api.bindevent($("form[role=form]"));

            $(document).on("click", ".charts-custom a[data-toggle=\"tab\"]", function () {
                var that = this;
                setTimeout(function () {
                    var id = $(that).attr("href");
                    var chart = Echarts.getInstanceByDom($(id)[0]);
                    chart.resize();
                }, 0);
            });
            var option;
            /*
            var months = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
            var legend = ['去年', '今年', '百分比'];
            var prevYear = '去年'
            var curYear = '今年'
            var maxPerson = 500;
            var maxPercent = 100;
            var prevPersons = [
                    2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3
                  ]
            var curPersons = [
                    2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3
                  ]
            var percent = [34.0, 80.2, 56.3, 87.5, 90, 60, 43.3, 67.4, 88.0, 59.5, 84.0, 30.2]
            */
            var yearToYearData = JSON.parse($("#personYearToYear").val());
            console.log('yearToYearData =>', yearToYearData)
            var barYearToYear = Echarts.init(document.getElementById('bar-chart-depart'), 'walden');
            option = {
              tooltip: {
                trigger: 'axis',
                axisPointer: {
                  type: 'shadow',
                },
                formatter:function(params){
                  // console.log(params)
                  return params[0].seriesName + '年' + params[0].name + "：" + params[0].data + '元<br/>' + params[1].seriesName + '年' + params[1].name + "：" + params[1].data + '元<br/>'+params[2].seriesName+ '：' + params[2].data + '%';
                  // return params[0].name  +'<br>生产量 '+ params[0].data;
                }

              },
              toolbox: {
                feature: {
                  saveAsImage: { show: true }
                }
              },
              legend: {
                data: yearToYearData.legend
              },
              xAxis: [
                {
                  type: 'category',
                  data: yearToYearData.months,
                  axisPointer: {
                    type: 'shadow'
                  }
                }
              ],
              yAxis: [
                {
                  type: 'value',
                  name: '费用(元)',
                  min: 0,
                  max: yearToYearData.maxPerson,
                  // interval: 5,
                  axisLabel: {
                    formatter: '{value}'
                  }
                },
                {
                  type: 'value',
                  name: '百分比',
                  min: 0,
                  max: yearToYearData.maxPercent,
                  // interval: 5,
                  axisLabel: {
                    formatter: '{value}%'
                  }
                }
              ],
              series: [
                {
                  name: yearToYearData.prevYear,
                  type: 'bar',
                  tooltip: {
                    valueFormatter: function (value) {
                      console.log('value=>', value)
                      return value + ' 人次';
                    }
                  },
                  data: yearToYearData.prevPersons 
                },
                {
                  name: yearToYearData.curYear,
                  type: 'bar',
                  tooltip: {
                    valueFormatter: function (value) {
                      return value + ' 人次';
                    }
                  },
                  data: yearToYearData.curPersons 
                },
                {
                  name: '同比',
                  type: 'line',
                  yAxisIndex: 1,
                  tooltip: {
                    valueFormatter: function (value) {
                      return value + ' %';
                    }
                  },
                  data: yearToYearData.percent 
                }
              ]
            };
            barYearToYear.setOption(option)
            /*
            var months = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
            var legend = ['今年', '百分比'];
            var curYear = '今年'
            var maxPerson = 500;
            var maxPercent = 100;
            var curPersons = [
                    2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3
                  ]
            var percent = [34.0, 80.2, 56.3, 87.5, 90, 60, 43.3, 67.4, 88.0, 59.5, 84.0, 30.2]
            */

            var personPercent = JSON.parse($("#personPercent").val());
            console.log('personPercent =>', personPercent)
            var barPercent = Echarts.init(document.getElementById('bar-chart-type'), 'walden');
            option = {
              tooltip: {
                trigger: 'axis',
                axisPointer: {
                  type: 'shadow',
                },
                formatter:function(params){
                  // console.log(params)
                  return "费用：" + params[0].data + '元<br/>' + params[1].seriesName+ '：' + params[1].data + '%';
                }

              },
              toolbox: {
                feature: {
                  saveAsImage: { show: true }
                }
              },
              legend: {
                data: personPercent.legend
              },
              xAxis: [
                {
                  type: 'category',
                  data: personPercent.months,
                  axisPointer: {
                    type: 'shadow'
                  }
                }
              ],
              yAxis: [
                {
                  type: 'value',
                  name: '人次',
                  min: 0,
                  max: personPercent.maxPerson,
                  // interval: 5,
                  axisLabel: {
                    formatter: '{value}'
                  }
                },
                {
                  type: 'value',
                  name: '百分比',
                  min: 0,
                  max: personPercent.maxPercent,
                  // interval: 5,
                  axisLabel: {
                    formatter: '{value}%'
                  }
                }
              ],
              series: [
                {
                  name: personPercent.curYear,
                  type: 'bar',
                  tooltip: {
                    valueFormatter: function (value) {
                      return value + ' 人次';
                    }
                  },
                  data: personPercent.curPersons 
                },
                {
                  name: '环比',
                  type: 'line',
                  yAxisIndex: 1,
                  tooltip: {
                    valueFormatter: function (value) {
                      return value + ' %';
                    }
                  },
                  data: personPercent.percent 
                }
              ]
            };
            barPercent.setOption(option)

            $(".btn-embossed").on("click", function() {
                queryDate = $("#query_date").val();
                Fast.api.ajax({
                    url:'pt/statistica/fee?query_date=' + queryDate
                }, function(data, ret){
                    console.log('statistic =>', ret.data)
                    var res = ret.data
                    yearToYearData = res['personYearToYear'];
                    personPercent = res['personPercent'];

                    barYearToYear.setOption({
                      legend: {
                        data: yearToYearData.legend
                      },
                      xAxis: [
                        {
                          type: 'category',
                          data: yearToYearData.months,
                          axisPointer: {
                            type: 'shadow'
                          }
                        }
                      ],
                      yAxis: [
                        {
                          type: 'value',
                          name: '费用(元)',
                          min: 0,
                          max: yearToYearData.maxPerson,
                          // interval: 5,
                          axisLabel: {
                            formatter: '{value}'
                          }
                        },
                        {
                          type: 'value',
                          name: '百分比',
                          min: 0,
                          max: yearToYearData.maxPercent,
                          // interval: 5,
                          axisLabel: {
                            formatter: '{value}%'
                          }
                        }
                      ],
                      series: [
                        {
                          name: yearToYearData.prevYear,
                          type: 'bar',
                          tooltip: {
                            valueFormatter: function (value) {
                              console.log('value=>', value)
                              return value + ' 人次';
                            }
                          },
                          data: yearToYearData.prevPersons 
                        },
                        {
                          name: yearToYearData.curYear,
                          type: 'bar',
                          tooltip: {
                            valueFormatter: function (value) {
                              return value + ' 人次';
                            }
                          },
                          data: yearToYearData.curPersons 
                        },
                        {
                          name: '同比',
                          type: 'line',
                          yAxisIndex: 1,
                          tooltip: {
                            valueFormatter: function (value) {
                              return value + ' %';
                            }
                          },
                          data: yearToYearData.percent 
                        }
                      ]
                    })
                    barPercent.setOption({
                      legend: {
                        data: personPercent.legend
                      },
                      xAxis: [
                        {
                          type: 'category',
                          data: personPercent.months,
                          axisPointer: {
                            type: 'shadow'
                          }
                        }
                      ],
                      yAxis: [
                        {
                          type: 'value',
                          name: '人次',
                          min: 0,
                          max: personPercent.maxPerson,
                          // interval: 5,
                          axisLabel: {
                            formatter: '{value}'
                          }
                        },
                        {
                          type: 'value',
                          name: '百分比',
                          min: 0,
                          max: personPercent.maxPercent,
                          // interval: 5,
                          axisLabel: {
                            formatter: '{value}%'
                          }
                        }
                      ],
                      series: [
                        {
                          name: personPercent.curYear,
                          type: 'bar',
                          tooltip: {
                            valueFormatter: function (value) {
                              return value + ' 人次';
                            }
                          },
                          data: personPercent.curPersons 
                        },
                        {
                          name: '环比',
                          type: 'line',
                          yAxisIndex: 1,
                          tooltip: {
                            valueFormatter: function (value) {
                              return value + ' %';
                            }
                          },
                          data: personPercent.percent 
                        }
                      ]
                    })
                }, function(){
                    Backend.api.toastr.error('统计错误');
                });
            })
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
